package com.legal.controller.chat;

import com.legal.domain.po.ChatMemoryMsg;
import com.legal.domain.result.Result;
import com.legal.service.chat.ChatService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

@RequestMapping("/ai")
@RestController
@RequiredArgsConstructor
@Tag(name = "聊天管理")  // OpenAPI 3.0 注解
public class ChatController {
    private final ChatService chatService;


    /**
     * 使用流式响应处理聊天消息
     * 该方法通过 POST 请求接收聊天记忆消息对象，并以流的形式返回聊天响应
     * 适用于需要实时或近实时更新聊天内容的场景
     *
     * @param chatMemoryMsg 聊天记忆消息对象，包含聊天所需的信息和上下文
     * @return Flux<String> 一个发布者 Flux，以字符串形式连续地产生聊天响应
     */
    @Operation(summary = "流式对话")  // OpenAPI 3.0 注解
    @PostMapping(value = "/chat/stream", produces = "text/html;charset=utf-8")
    public Flux<String> chat(@RequestBody ChatMemoryMsg chatMemoryMsg){
        return chatService.chatStream(chatMemoryMsg);
    }

    @GetMapping("/test")
//    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @PreAuthorize("hasRole('ROLE_USER')")
    public Result<String> test() {
        return Result.ok("test");
    }


}
